<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="日记名称">
              <a-input placeholder="请输入日记名称" v-model="queryParam.title" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="时间筛选">
              <a-range-picker @change="panelChange" v-model="queryParamRangePicker"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="评分">
              <a-select default-value="1" placeholder="全部评分" v-model="scoreVal" @change="scoreChange()">
                <a-select-option :value="item.val" v-for="(item, index) in scoreUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="状态">
              <a-select default-value="1" placeholder="全部状态" v-model="queryParam.status" >
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="用户名">
              <a-input placeholder="请输入用户名" v-model="queryParam.name" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="手机号">
              <a-input placeholder="请输入手机号" v-model="queryParam.mobile" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="日记等级">
              <a-select default-value="1" placeholder="全部等级" v-model="queryParam.grade" >
                <a-select-option :value="item.val" v-for="(item, index) in gradeUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="24" :lg="24" :md="24" :sm="24">
            <div class="tag_box">
              <div class="tag_box_flex">
                <div class="tag_box_flex_title">
                  <span class="tag_box_title">用户标签：</span>
                </div>
                <div class="tag_box_flex_li" :class="!tagShow ? 'tag_box_flex_show':''">
                  <span class="tag_box_flex_btn" id="tag_box_flex_btn">
                    <a-button 
                    class="tag_button"
                    v-for="(item , index) in tagUl" 
                    size="small"
                    :key="index" 
                    :type="item.choose ? 'primary' : '' " 
                    @click="handleTagsChange(index)"> {{item.name}} </a-button>
                    <a-button size="small" id="tag_show_btn1" class="tag_show_btn1" type="primary" @click="funTagShow" v-show="!tagShow">展开</a-button>
                    <a-button size="small" id="tag_show_btn2" type="primary" @click="funTagShow" v-show="tagShow">收起</a-button>
                  </span>
                </div>
              </div>
            </div>
          </a-col>
          <a-col :xl="24" :lg="24" :md="24" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons" >
              <a-button type="primary" @click="searchQuery" icon="search" >查询</a-button >
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px" >重置</a-button >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="crudCreate()" type="primary" icon="plus">新增</a-button> -->
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id"  :scroll="{ x: 1300 }" :columns="columns" :dataSource="tableData" :pagination="ipagination" :loading="loading" @change="handleTableChange" >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="handleDetail(record)">详情</a-button>
          <a-popconfirm title="确认删除?" ok-text="确定" cancel-text="取消" @confirm="crudDelete(record.id)">
            <a-button type="danger" icon="delete"></a-button>
          </a-popconfirm>
        </span>
        <!--评分-->
        <span slot="score" slot-scope="text, record">
          <a-rate v-model="record.score" disabled allowHalf/>
        </span>
        <!--标签-->
        <span slot="tagList" slot-scope="text, record">
          <a-tag class="tag_li" color="green" v-for="(item,index) in record.tagList" :key="index">{{item}}</a-tag>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <diary-list-modal ref="modalForm"></diary-list-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import diaryListModal from './modules/diaryListModal'
import ApiTree from "@/api/diary/diaryList";
import TagList from "@/api/common/tagList";
import { Slime, crud } from "@/mixins/slime";

export default {
  name: "diaryList",
  mixins: [Slime],
  components: {
    diaryListModal
  },
  data() {
    return {
      scoreUl: [
        { name: "4.0-5.0", val: 1 },
        { name: "3.0-4.0", val: 2 },
        { name: "2.0-3.0", val: 3 },
        { name: "2.0及以下", val: 4 },
      ],
      statusUl: [
        { name: "待审核", val: 0 },
        { name: "审核通过", val: 10 },
        { name: "审核不通过", val: 20 },
      ],
      gradeUl: [
        { name: "普通日记", val: 1 },
        { name: "小编精选", val: 2 },
        { name: "首页推荐", val: 3 },
      ],
      //tag
      tagUl:[
        // {name: "", val:1, choose:false}
      ],
      tagUlValue:[],
      tagShow: false,
      tableData: [],
      columns: [
        {
          title: "标题",
          align: "center",
          dataIndex: "title",
        },
        {
          title: "来源",
          align: "center",
          dataIndex: "source",
          customRender: function (source) {
            switch (source) {
              case 0:
                return "会员添加";
              case 1:
                return "商家添加";
              default:
                return "未知状态";
            }
          },
        },
        {
          title: '用户名',
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'mobile',
        },
        {
          title: "标签",
          align: "center",
          dataIndex: "tagList",
          width: 100,
          scopedSlots: { customRender: 'tagList' },
        },
        {
          title: '日志等级',
          align: 'center',
          dataIndex: 'grade',
          customRender: function (grade) {
            switch (grade) {
              case 1:
                return "普通日记";
              case 2:
                return "小编精选";
              case 3:
                return "首页推荐";  
              default:
                return "未设置";
            }
          },
        },
        {
          title: "状态",
          align: "center",
          dataIndex: "status",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "待审核";
              case 10:
                return "审核通过";
              case 20:
                return "审核不通过";
              default:
                return "未知状态";
            }
          },
        },
        {
          title: "发布时间",
          align: "center",
          dataIndex: "createTime",
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      scoreVal:null,
    };
  },
  created() {
    crud(ApiTree);
    this.loadData();
    this.tagList()
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData.map((obj)=>{
          return obj = {
            ...obj,
            score: obj.score/10,
          }
        })
      });
    },
    //标签
    tagList(){
      let param = {
        type:2
      }
      TagList.getList(param).then((res) => {
        let tagUl = []
        tagUl = res.result.map((obj)=>{
          return obj = {
            val: obj.id, 
            name: obj.name,
            choose: false,
          }
        })
        this.tagUl = tagUl
      })
    },
    //选择标签
    handleTagsChange(index){
      this.tagUl[index].choose = !this.tagUl[index].choose
      let tagIds = []
      for(let i=0;i<this.tagUl.length;i++){
        if(this.tagUl[i].choose){
          tagIds.push(this.tagUl[i].val)
        }
      }
      this.queryParam.tagIds = tagIds.join(',')
      // this.tagUlValue = value
      // let tagIds = value.join(',')
      // this.queryParam.tagIds = tagIds
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.tagUl = this.tagUl.map((obj)=>{
        return obj = {
          ...obj,
          choose:false,
        }
      })
      this.queryParamRangePicker = null;
      this.$router.push({ query: {} })
      this.loadData(1);
    },
    funTagShow(){
      this.tagShow = !this.tagShow
    },
    //详情
    handleDetail(record){
      this.$router.push({ path: '/diaryDetail?id=' + record.id})
    },
    //搜索评分变化
    scoreChange(){
      let val = this.scoreVal
      if(val == 1){
        this.queryParam.startScore = 40
        this.queryParam.endScore = 50
      }else if(val == 2){
        this.queryParam.startScore = 30
        this.queryParam.endScore = 40
      }else if(val == 3){
        this.queryParam.startScore = 20
        this.queryParam.endScore = 30
      }else if(val == 4){
        this.queryParam.startScore = 0
        this.queryParam.endScore = 20
      }
    },
    //改变筛选时间
    panelChange(date){
      let startTime = moment(date[0]).format('YYYY-MM-DD');
      let endTime = moment(date[1]).format('YYYY-MM-DD');
      this.queryParam = {...this.queryParam, startCreateDate:startTime, endCreateDate:endTime}
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.queryParamRangePicker = null;
      //new
      this.scoreVal = null
      this.$router.push({ query: {} })
      this.loadData(1);
    },
    funUsed(status) {
      switch (status) {
        case 0:
          return "初始状态";
        case 1:
          return "待审核";
        case 2:
          return "审核通过";
        case 3:
          return "审核不通过";
        case 4:
          return "已下架";
        case 5:
          return "已上架";
        default:
          return "未知状态";
      }
    },
  },
};
</script>
<style scoped>
/* .ant-rate{
  color: #cc0000;
} */
</style>